package com.pacifictextiles.qc.inspection.server.controller.servlets;

//当前算法并未说明 5，6怎么分

import java.util.LinkedList;
import java.util.List;

import com.pacifictextiles.qc.inspection.server.model.dao.impl.DefectDAOImpl;
import com.pacifictextiles.qc.inspection.server.model.dao.impl.RollDAOImpl;
import com.pacifictextiles.qc.inspection.server.model.vo.Defect;

public class CalculatePoints {

	float averageOfPoint;
	List<Defect> defeList = new LinkedList<Defect>();
	int qualityLevel;
	boolean isSuccess = false;

	
	// 给入当前rollNo 和 判定的等级 更新成功return true
	public boolean calPointPressKey(String rollNo, int qualityLevel) throws Exception {

		// 准备数据库操作对象
		RollDAOImpl rdi = new RollDAOImpl();

		// 更新布匹等级
		setQualityLevel(qualityLevel);
		if (rdi.updateQualityLevel(rollNo, getQualityLevel())) {
			// 更新成功 设置 isSuccess 为 true
			isSuccess = true;
			System.out.print("CalculatePoints:  计算分数人工打分：  布匹等级更新成功" + "\n");
		} else {
			System.out.print("CalculatePoints:  计算分数人工打分：  布匹等级更新失败" + "\n");
		}
		return isSuccess;
	}
	
	// 给入当前rollNo 和 码长，计算出每百码平均分，判断等级 更新成功return true
	public boolean calPoint(String rollNo, String currentLength) throws Exception {
		
		//准备数据库操作对象
		DefectDAOImpl ddi = new DefectDAOImpl();
		RollDAOImpl rdi = new RollDAOImpl();

		
		// 获得此时在缸里的defect list
		defeList = ddi.getDefectList(rollNo);
		// 计算当前每百码平均分，如果得到-1,说明未接收到码长
		//直接返回false
		averageOfPoint = calAverage(defeList, currentLength);
		if (averageOfPoint == -1) {
			System.out.print("CalculatePoints:  计算分数： 接收码长失败\n布匹等级更新失败" + "\n");
			return isSuccess;
		}
		//算得每百码平均分，给出评级
		giveQualityLevel(averageOfPoint);

		
		// 更新布匹等级
		if (rdi.updateQualityLevel(rollNo, getQualityLevel())) {
			//更新成功 设置 isSuccess 为 true
			isSuccess = true;
			System.out.print("CalculatePoints:  计算分数：  布匹等级更新成功" + "\n");
		} else {
			System.out.print("CalculatePoints:  计算分数：  布匹等级更新失败" + "\n");
		}
		return isSuccess;
	}


	//计算每百码分数
	private float calAverage(List<Defect> defeList,String currentLength) {
		
		int sum = 0;	
		// 当前总分值
		for (Defect defect : defeList) {
			sum += defect.getPoint();
			System.out.print("The current defect point"+defect.getPoint()+"\n");
		}
		
		
		// 设置变量，接收当前码长
		float curLength;
		// 如果未接收到 或者接受到一个空值
		if (currentLength == null || currentLength == "") {
			curLength = -1;
			// 未接收到码长则return负数
			return curLength;
		} else {
			curLength = Float.parseFloat(currentLength);
		}

		
		// 接受当前码长 设置到 averageOfPoint
		setaverageOfPoint((sum / curLength) * 100);
		return averageOfPoint;
	}

	
	//给出等级 在之前应该先给出判断平均数正常与否
	private void giveQualityLevel(float averageOfPoint) {

		if (averageOfPoint < 25.00) {
			setQualityLevel(1);
		} else if (averageOfPoint >= 25.00 && averageOfPoint < 35.00) {
			setQualityLevel(2);
		} else if (averageOfPoint >= 35.00 && averageOfPoint < 45.00) {
			setQualityLevel(3);
		} else if (averageOfPoint >= 45.00 && averageOfPoint < 55.00) {
			setQualityLevel(4);
		} 
		//当前算法并未说明 5，6怎么分
		else {
			setQualityLevel(5);
		}
	}

	
	public void setQualityLevel(int qualityLevel) {
		this.qualityLevel = qualityLevel;
	}

	public int getQualityLevel() {

		return this.qualityLevel;
	}

	
	public void setaverageOfPoint(float averageOfPoint) {

		this.averageOfPoint = averageOfPoint;
	}

	public float getaverageOfPoint() {

		return this.getaverageOfPoint();
	}
}
